package info.heidao.ww2.common.response;

import cn.hutool.core.date.SystemClock;
import info.heidao.ww2.common.constants.ResultCodeEnum;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * 接口json应答
 *
 * @author lintw
 * @date 2020/3/14
 */
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JsonResponse<T> {
    /**
     * 结果值
     */
    private String code;
    /**
     * 结果消息
     */
    private String msg;
    /**
     * 数据
     */
    private T data;
    /**
     * 当前服务器时间
     */
    private final long serverTime = SystemClock.now();

    private JsonResponse(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private JsonResponse(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static JsonResponse<Void> build(String code, String msg) {
        return new JsonResponse<>(code, msg);
    }

    public static <T> JsonResponse<T> build(String code, String msg, T data) {
        return new JsonResponse<>(code, msg, data);
    }

    public static <T> JsonResponse<T> success() {
        return success(ResultCodeEnum.SUCCESS.getMsg(), null);
    }

    public static <T> JsonResponse<T> success(T data) {
        return success(ResultCodeEnum.SUCCESS.getMsg(), data);
    }

    public static <T> JsonResponse<T> success(String msg, T data) {
        return build(ResultCodeEnum.SUCCESS.getCode(), msg, data);
    }
}
